package com.dui;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class Panel extends JPanel {
    /*
     * 游戏状态 0未开始 1游戏中 2 通关失败 3 通关成功 4暂停*/
    int stateG = 0;
    Image bgImg = new ImageIcon("img/bg.jpg").getImage();
    Image adImg = new ImageIcon("img/ad.png").getImage();
    List<Beetle> beetles = new ArrayList<Beetle>();
    Dui dui = new Dui();

    public Panel(){
        setFocusable(true); // 允许面板获取焦点
        requestFocusInWindow(); // 主动请求焦点
        this.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                if(e.getButton()==1&&stateG==0){
                    stateG=1;
                    repaint();
                }

            }
        });
        this.addKeyListener(new KeyAdapter() {
            @Override//按压
            public void keyPressed(KeyEvent e) {
                super.keyPressed(e);
               // System.out.println(dui.state);
                if(e.getKeyCode()==KeyEvent.VK_W){
                    dui.y = dui.y - dui.speed;
                }
                if(e.getKeyCode()==KeyEvent.VK_S){
                    dui.y = dui.y + dui.speed;

                }
                if(e.getKeyCode()==KeyEvent.VK_A){
                    dui.state=0;
                    dui.x = dui.x - dui.speed;
                }
                if(e.getKeyCode()==KeyEvent.VK_D){
                    dui.x = dui.x + dui.speed;
                    dui.state=1;
                }
            }

            @Override
            public void keyReleased(KeyEvent e) {
                super.keyReleased(e);
            }
        });

    }
    public void paint(Graphics g){
        super.paint(g);
        //绘制背景图
        g.drawImage(bgImg,0,0,null);
        //绘制蟑螂
        paintBeetle(g);
        //切换页面状态
        changeState(g);
        if(stateG==3){
            g.drawImage(adImg,0,0,null);
        }
        //绘制分数
        paintScore(g);
    }


    public void action(){
        java.util.Timer timer = new Timer();
        timer.schedule(new TimerTask() {

            @Override
            public void run() {
                //创建蟑螂
                beetleAction();
                //移动蟑螂
                beetleMoveAndStep();
                //堆堆吃蟑螂
                duiEatBeetle();
                //重绘
                repaint();
            }
        },0,10);
    }


    //切换页面状态
    public void changeState(Graphics g){
        switch(stateG){
            case 0:
                g.drawImage(bgImg,0,0,null);
                g.setColor(Color.decode("#E8DCCA"));
                g.setFont(new Font("楷书",Font.BOLD,60));
                g.drawString("开始",1000,650);
                break;
            case 1:
                //请出堆堆
                paintDui(g);
                g.setColor(Color.decode("#E8DCCA"));
                g.setFont(new Font("楷书",Font.BOLD,30));
                g.drawString("目标：吃掉10只邪恶的坏蟑螂",600,50);
                break;
            case 2:
                break;
            case 3:
                g.drawImage(adImg,0,0,null);
                g.drawImage(bgImg,0,0,null);
                g.setColor(Color.decode("#E8DCCA"));
                g.setFont(new Font("楷书",Font.BOLD,60));
                g.drawString("堆堆大胜利",700,500);
                g.drawString("你也来一只吧~",700,650);
                break;
            case 4:
                break;

        }
    }
    //绘制分数
    public void paintScore(Graphics g){
        g.setColor(Color.decode("#E8DCCA"));
        g.setFont(new Font("楷书",Font.BOLD,40));
        g.drawString("分数:"+dui.score,1000,100);
        g.drawString("等级:"+dui.level,1000,150);
        g.drawString("速度:"+dui.speed,1000,200);
        g.drawString("状态:"+dui.state,1000,250);

    }

    //画出蟑螂
    private void paintBeetle(Graphics g) {
        for (int i = 0; i < beetles.size(); i++) {
            Beetle beetle = beetles.get(i);
            g.drawImage(beetle.beetleImg,beetle.x,beetle.y,null);
        }

    }
    //请出堆堆
    private void paintDui(Graphics g) {

        g.drawImage(dui.duiImg, dui.x,dui.y,null);

        dui.step();
    }

    //创建蟑螂
    int beetleIndex = 0;
    public void beetleAction(){
        if(beetleIndex==Integer.MAX_VALUE){
            beetleIndex = 0;
        }
        if(beetleIndex++%15==0){
            beetles.add(new Beetle());
        }
    }
    //移动蟑螂
    public void beetleMoveAndStep(){
        for(int i=0;i<beetles.size();i++){
            Beetle beetle = beetles.get(i);
            beetle.moveAndStep();
        }
    }
    private void duiEatBeetle() {
        if (stateG ==0 ||stateG==3 ) return;

        for(int i=0;i<beetles.size();i++){
            Beetle beetle = beetles.get(i);
            if(dui.eatBeetle(beetle)){
                dui.score=dui.score+ beetle.count;
                dui.speed += beetle.count*2;
                beetles.remove(beetle);
            }
        }
        if(dui.score>=10){
            stateG=3;
            repaint();
            return;
        }
    }

}
